package com.zjsru.studyplan1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 无重复字符的最长子串
 *
 * 输入: s = "abcabcbb"
 * 输出: 3
 * 解释: 因为无重复字符的最长子串是 "abc"，所以其长度为 3。
 *
 * */
public class lengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        Set<Character> set = new HashSet<Character>();
        int rk = -1 , ans = 0;
        for (int i = 0; i < len; i++) {
            if(i != 0){
                //移除前一位
                set.remove(s.charAt(i-1));
            }
            while(rk +1< len && !set.contains(s.charAt(rk+1))){
                set.add(s.charAt(rk +1));
                rk ++;
            }
            ans = Math.max(ans, rk - i +1);
        }
        return ans;
    }

    public static void main(String[] args){
        String s = "abcabcbb";
        lengthOfLongestSubstring lengthOfLongestSubstring = new lengthOfLongestSubstring();
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(s));
    }
}
